package cn.fresh.xuanyuan.module.pay.api.order;

import cn.fresh.xuanyuan.framework.common.util.json.JsonUtils;
import cn.fresh.xuanyuan.framework.pay.core.client.PayClientConfig;
import cn.fresh.xuanyuan.framework.pay.core.client.impl.weixin.WxPayClientConfig;
import cn.fresh.xuanyuan.framework.pay.core.enums.channel.PayChannelEnum;
import cn.fresh.xuanyuan.module.pay.api.order.dto.PayOrderDto;
import cn.fresh.xuanyuan.module.pay.controller.admin.channel.vo.PayChannelRespVO;
import cn.fresh.xuanyuan.module.pay.convert.order.PayOrderConvert;
import cn.fresh.xuanyuan.module.pay.dal.dataobject.channel.PayChannelDO;
import cn.fresh.xuanyuan.module.pay.dal.dataobject.order.PayOrderDO;
import cn.fresh.xuanyuan.module.pay.service.channel.PayChannelService;
import cn.fresh.xuanyuan.module.pay.service.order.PayOrderService;
import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.fresh.xuanyuan.module.pay.api.order.dto.PayOrderRespDTO;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static cn.fresh.xuanyuan.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.fresh.xuanyuan.framework.common.pojo.CommonResult.success;
import static cn.fresh.xuanyuan.module.pay.enums.ErrorCodeConstants.CHANNEL_NOT_FOUND;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class PayOrderApiImpl implements PayOrderApi {

    @Resource
    private PayOrderService payOrderService;

    @Resource
    private PayChannelService payChannelService;

    @Override
    public CommonResult<Long> createOrder(PayOrderCreateReqDTO reqDTO) {
        return success(payOrderService.createOrder(reqDTO));
    }

    @Override
    public CommonResult<PayOrderRespDTO> getOrder(Long id) {
        PayOrderDO order = payOrderService.getOrder(id);
        return success(PayOrderConvert.INSTANCE.convert2(order));
    }

    @Override
    public CommonResult<Boolean> updatePayOrderPrice(Long id, Integer payPrice) {
        payOrderService.updatePayOrderPrice(id, payPrice);
        return success(true);
    }

    @Override
    public CommonResult<PayOrderDto> getPayOrder(String orderId) {
        PayOrderDO payOrderDO = payOrderService.getOrder(orderId);
        if(payOrderDO != null) {
            PayOrderDto payOrderDto = new PayOrderDto();
            BeanUtils.copyProperties(payOrderDO, payOrderDto);
            return success(payOrderDto);
        }
        return success(null);
    }

    @Override
    public CommonResult<String> getPayAppId(String orderId) {
        PayOrderDO payOrderDO = payOrderService.getOrder(orderId);
        if(payOrderDO != null && payOrderDO.getChannelId() != null) {
            PayChannelRespVO payChannelDO = payChannelService.getPayChannelById(payOrderDO.getChannelId());
            if(StringUtils.isNotBlank(payChannelDO.getConfig())) {
                WxPayClientConfig config = JsonUtils.parseObject2(payChannelDO.getConfig(), WxPayClientConfig.class);
                return success(config.getAppId());
            }
        }
        return success(null);
    }

}
